package com.library.service.Impl;

import com.library.context.BaseContext;
import com.library.entity.Follow;
import com.library.entity.User;
import com.library.mapper.FollowMapper;
import com.library.mapper.UserMapper;
import com.library.service.SocializeService;
import com.library.service.UserService;
import com.library.vo.UserProfileVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SocializeServiceImpl implements SocializeService {

    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BasicErrorController basicErrorController;

    @Override
    public UserProfileVO getUserProfile(Long userId) {
        User user = userMapper.getById(userId);
        if (user != null) {
            UserProfileVO userProfileVO = UserProfileVO.builder()
                    .id(user.getId())
                    .username(user.getUsername())
                    .avatar(user.getAvatar())
                    .biography(user.getBiography())
                    .fans(followMapper.countFans(userId))
                    .follows(followMapper.countFollows(userId))
                    .build();
            return userProfileVO;
        }
        return null;
    }

    @Override
    public void follow(Long userId) {
        Follow follow = Follow.builder()
                .userId(BaseContext.getCurrentId())
                .followUserId(userId)
                .build();
        followMapper.follow(follow);
    }

    @Override
    public List<UserProfileVO> getFollowers(Long userId) {
        List<UserProfileVO> userProfileVOList = followMapper.getFollowers(userId);
        if (userProfileVOList != null) {
            return userProfileVOList;
        }
        return null;
    }

    @Override
    public List<UserProfileVO> getFollowing(Long userId) {
        List<UserProfileVO> userProfileVOList = followMapper.getFollowing(userId);
        if (userProfileVOList != null) {
            return userProfileVOList;
        }
        return null;
    }


}
